package com.rxmvp.scrollerdemo.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * 作者： huangRZ  日期： 2017/2/9. email：917647409@qq.com
 * Explain：
 */

public class MyListView extends ViewGroup {

    private float mDownY;
    private float mLastMoveY;
    private int mTouchSlop;
    private float mMoveY;
    private int mTopBorder;
    private int mBottomBorder;
    private Scroller mScroller;
    private VelocityTracker mVelocityTracker;
    private int mPointerId;

    public MyListView(Context context) {
        this(context, null);
    }

    public MyListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        super.setClickable(true);//设置该控件可以点击
        //初始化操作
        //获取判断为滑动移动的最小像素数
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        //获取Scroller实例
        mScroller = new Scroller(context);
//        //获取VelocityTracker实例
//        mVelocityTracker = VelocityTracker.obtain();
    }

    @Override
    protected void onFinishInflate() {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            measureChildren(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed) {
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View view = getChildAt(i);
                view.layout(0, i * view.getMeasuredHeight(), view.getMeasuredWidth(), (i + 1) * view.getMeasuredHeight());
            }
            mTopBorder = getChildAt(0).getTop();
            mBottomBorder = getChildAt(childCount - 1).getBottom();
            Log.e("listView", "onLayout: top :" + mTopBorder + "  bottom: " + mBottomBorder);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e("listView", "onTouchEvent: down");
                mDownY = ev.getRawY();
                mLastMoveY = mDownY;
                //如果动画没结束，则停止动画
                if(mScroller != null && !mScroller.isFinished()){
                        mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                mMoveY = ev.getRawY();
                if (Math.abs(mLastMoveY - mMoveY) > mTouchSlop) {
                    mLastMoveY = mMoveY;
                    return true;
                }
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //获得VelocityTracker对象，并且添加滑动对象
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //求第一个触点的id， 此时可能有多个触点，但至少一个
//                mPointerId = event.getPointerId(0);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("listView", "onTouchEvent: move");
                mMoveY = event.getRawY();
                //求出手指在竖直方向上划过的距离
                int offset = (int) (mLastMoveY - mMoveY);
                if (offset + getScrollY() < mTopBorder) {
                    //超出顶部
                    scrollTo(0, mTopBorder);
                    return true;
                } else if (offset + getScrollY() + getHeight()> mBottomBorder){
                    scrollTo(0, mBottomBorder - getHeight());
                    return true;
                }
                scrollBy(0, offset);
                mLastMoveY = mMoveY;
                break;
            case MotionEvent.ACTION_UP:
                Log.e("listView", "onTouchEvent: up :" + (mBottomBorder - getHeight()));
                //计算当前手指抬起时的速度
                mVelocityTracker.computeCurrentVelocity(1000);
                //获取竖直方向上的速度
                float yVelocity = mVelocityTracker.getYVelocity();
                mScroller.fling(0, getScrollY(), 0, -(int)yVelocity, 0, getWidth(), 0, mBottomBorder - getHeight());

                //回收VelocityTracker对象
                if (mVelocityTracker != null) {
                    mVelocityTracker.recycle();
                    mVelocityTracker = null;
                }
                invalidate();//重新绘制，触发4computeScroll方法
                break;
        }

        return super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()){
            Log.e("listView", "onTouchEvent: fling" + "  Y : " + getScrollY());
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }
}
